package com.ht.baselibs.retrofit.interceptor;


import com.ht.baselibs.utils.LogUtils;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by huangtao on 16/7/11.
 */
public class HttpLogInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String method = request.method();
        String requestMessage = "--> " + method + ' ' + request.url() + ' ';
        if ("POST".equals(method)){
            FormBody body = (FormBody) request.body();
            for (int i = 0; i < body.size(); i++) {
                String paramName = body.name(i);
                String paramValue = body.value(i);
                requestMessage += '\n'+ paramName+"="+paramValue;
            }
        }
        LogUtils.error(requestMessage);
        Response response = chain.proceed(request);
        String responseMessage = "<-- "+response.code()+' '+response.message()+' '+response.request().url();
        LogUtils.error(responseMessage);
        return response;
    }
}